package com.ruoyi.activity.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.activity.domain.BmPreferentialGoods;
import com.ruoyi.activity.mapper.BmPreferentialMapper;
import com.ruoyi.activity.domain.BmPreferential;
import com.ruoyi.activity.service.IBmPreferentialService;

/**
 * 优惠活动信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-08
 */
@Service
public class BmPreferentialServiceImpl implements IBmPreferentialService 
{
    @Autowired
    private BmPreferentialMapper bmPreferentialMapper;

    /**
     * 查询优惠活动信息
     * 
     * @param id 优惠活动信息主键
     * @return 优惠活动信息
     */
    @Override
    public BmPreferential selectBmPreferentialById(Long id)
    {
        return bmPreferentialMapper.selectBmPreferentialById(id);
    }

    /**
     * 查询优惠活动信息列表
     * 
     * @param bmPreferential 优惠活动信息
     * @return 优惠活动信息
     */
    @Override
    public List<BmPreferential> selectBmPreferentialList(BmPreferential bmPreferential)
    {
        return bmPreferentialMapper.selectBmPreferentialList(bmPreferential);
    }

    /**
     * 新增优惠活动信息
     * 
     * @param bmPreferential 优惠活动信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBmPreferential(BmPreferential bmPreferential)
    {
        int rows = bmPreferentialMapper.insertBmPreferential(bmPreferential);
        insertBmPreferentialGoods(bmPreferential);
        List<BmPreferentialGoods> bmPreferentialGoodsList = bmPreferential.getBmPreferentialGoodsList();
        for (BmPreferentialGoods bmPreferentialGoods : bmPreferentialGoodsList) {
            bmPreferentialMapper.updatePreferentialGoodsPrice(bmPreferentialGoods);
        }
        return rows;
    }

    /**
     * 修改优惠活动信息
     * 
     * @param bmPreferential 优惠活动信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBmPreferential(BmPreferential bmPreferential)
    {
        bmPreferentialMapper.deleteBmPreferentialGoodsByPreferentialId(bmPreferential.getId());
        insertBmPreferentialGoods(bmPreferential);
        List<BmPreferentialGoods> bmPreferentialGoodsList = bmPreferential.getBmPreferentialGoodsList();
        for (BmPreferentialGoods bmPreferentialGoods : bmPreferentialGoodsList) {
            bmPreferentialMapper.updatePreferentialGoodsPrice(bmPreferentialGoods);
        }
        return bmPreferentialMapper.updateBmPreferential(bmPreferential);
    }

    /**
     * 批量删除优惠活动信息
     * 
     * @param ids 需要删除的优惠活动信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBmPreferentialByIds(Long[] ids)
    {
        bmPreferentialMapper.deleteBmPreferentialGoodsByPreferentialIds(ids);
        return bmPreferentialMapper.deleteBmPreferentialByIds(ids);
    }

    /**
     * 删除优惠活动信息信息
     * 
     * @param id 优惠活动信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBmPreferentialById(Long id)
    {
        bmPreferentialMapper.deleteBmPreferentialGoodsByPreferentialId(id);
        return bmPreferentialMapper.deleteBmPreferentialById(id);
    }

    /**
     * 新增#优惠活动商品信息信息
     * 
     * @param bmPreferential 优惠活动信息对象
     */
    public void insertBmPreferentialGoods(BmPreferential bmPreferential)
    {
        List<BmPreferentialGoods> bmPreferentialGoodsList = bmPreferential.getBmPreferentialGoodsList();
        Long id = bmPreferential.getId();
        if (StringUtils.isNotNull(bmPreferentialGoodsList))
        {
            List<BmPreferentialGoods> list = new ArrayList<BmPreferentialGoods>();
            for (BmPreferentialGoods bmPreferentialGoods : bmPreferentialGoodsList)
            {
                bmPreferentialGoods.setPreferentialId(id);
                list.add(bmPreferentialGoods);
            }
            if (list.size() > 0)
            {
                bmPreferentialMapper.batchBmPreferentialGoods(list);
            }
        }
    }
}
